# Project Jocara

import pygame
from pygame.locals import *

import system

BUTTON_NEW_GAME = 1
BUTTON_LOAD_GAME = 2
BUTTON_OPTIONS = 4
BUTTON_EXIT = 3
BUTTON_OK = 10

class Button(pygame.sprite.Sprite):
    def __init__(self, font, text, n_id, color=pygame.Color('white')):
        super().__init__()
        
        self.mouse_over = False
        
        self._state = False
        self._text = text
        self._color = color
        self._font = font
        self._screen = pygame.display.get_surface() # Fix this
        self._id = n_id
        
        self._draw()
        self.rect = self.image.get_rect(center=(
                            self._screen.get_width()/2,
                            self._screen.get_height()/2))

    def update(self):
        if self.mouse_over and not self._state:
            self._draw()
            self._state = True
        elif not self.mouse_over and self._state:
            self._draw()
            self._state = False
            
    def _draw(self):
        self.image = pygame.Surface((270,60))
        if self.mouse_over:
            self.image.fill((0,64,0))
        else:
            self.image.fill(pygame.Color('black'))
        
        text = self._font.render(self._text, True, self._color)
        text_pos = text.get_rect(center=(
                            self.image.get_width()/2,
                            self.image.get_height()/2))
        self.image.blit(text,text_pos.topleft)


class Text(pygame.sprite.Sprite):
    def __init__(self, font, text, color=pygame.Color('white')):
        super().__init__()
        screen = pygame.display.get_surface() # Fix this
        self.image = font.render(text, True, color)
        self.rect = self.image.get_rect(center=(
                            screen.get_width()/2,
                            screen.get_height()/2))


class Message(pygame.sprite.Sprite):
    def __init__(self, font, text, color=pygame.Color('white')):
        super().__init__()
        
        screen = pygame.display.get_surface() # Fix this
        self.image = pygame.Surface((400,160)).convert_alpha()
        self.image.fill((60,)*3)
        pygame.draw.rect(self.image, (0,)*4,
                         pygame.Rect(65,
                                     self.image.get_height()/2,
                                     270,60))
        
        text = font.render(text, True, color)
        text_pos = text.get_rect(center=(
                            self.image.get_width()/2,
                            self.image.get_height()/2-35))

        self.image.blit(text,text_pos.topleft)
        self.rect = self.image.get_rect(center=(
                            screen.get_width()/2,
                            screen.get_height()/2))

def mouse_track(group):
    mouse_position = pygame.mouse.get_pos()
    for sprite in group.sprites():
        sprite.mouse_over = sprite.rect.collidepoint(mouse_position)
        
def get_button_pressed(group):
    mouse_position = pygame.mouse.get_pos()
    for sprite in group.sprites():
        if sprite.rect.collidepoint(mouse_position):
            return sprite._id
    return None
    
def load_page(page):
    gui.empty()
    mouse_subscribers.empty()
    if page is 0:
        # Main Menu
        buttons = (
                Button(system.font[0],'New Game', 1),					
                Button(system.font[0],'Load Game', 2),				
                Button(system.font[0],'Options', 4),
                Button(system.font[0],'Exit', 3)
                )
				    
        buttons[0].rect.top -= 120
        buttons[1].rect.top -= 60
        buttons[3].rect.top += 60
        
        static = ()
        
    elif page is 1:
        # Not Implemented Message
        static = (Message(system.font[1],'Sorry, no can do.'),)
        buttons = (Button(system.font[0],'OK', 10),)		
        buttons[0].rect.top += 30
        
    gui.add(static+buttons)
    mouse_subscribers.add(buttons)

def main_menu():
    '''Main menu of the game'''
    global gui, mouse_subscribers
    # Background
    background = pygame.Surface(system.screen.get_size())
    background.fill((30,)*3)

    gui = pygame.sprite.RenderUpdates()
    mouse_subscribers = pygame.sprite.Group()
    
    load_page(0)
    
    while True:
        system.clock.tick(60)

        for event in pygame.event.get():
            if event.type == QUIT:
                return 0
            #elif event.type == KEYDOWN and event.key == K_ESCAPE:
            #    return 0
            elif event.type is MOUSEBUTTONUP:
                button_pressed = get_button_pressed(mouse_subscribers)
                if button_pressed is BUTTON_EXIT:
                    return 0
                elif button_pressed is BUTTON_OK:
                    load_page(0)
                elif button_pressed is BUTTON_NEW_GAME:
                    return 2
                elif button_pressed in (BUTTON_LOAD_GAME,BUTTON_OPTIONS):
                    load_page(1)
        
        mouse_track(mouse_subscribers)
        
        system.screen.blit(background, (0,0))
        
        gui.update()
        gui.draw(system.screen)
        
        pygame.display.flip()
        